package com.fw;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.fw.entity.Permission;
import com.fw.entity.Role;
import com.fw.mapper.RoleMapper;

public class RoleMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    public RoleMapperTest() {
        try {
            // Load MyBatis configuration file
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

     public void testFindRoleWithPermissionsById(){
         SqlSession sqlSession = null;
         // Get SqlSession
        sqlSession = sqlSessionFactory.openSession();
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        Role role=roleMapper.findRoleWithPermissionsById(1);
        System.out.println(role);
        //判断role是否为空
        if(role!=null){
            if (role.getPermissions()==null || role.getPermissions().size()==0) {
                System.out.println(""+role.getRolename()+"没有权限");
            }
            //查询role下的权限
            for(Permission permission:role.getPermissions()){
                System.out.println(permission);
            }
        }
        

     }
     public static void main(String[] args) {
        RoleMapperTest test=new RoleMapperTest();
        test.testFindRoleWithPermissionsById();
     }

}
